home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / gitkeys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-06  |  8.1 KB  |  405 lines

  1. /* gitkeys.c -- An utility designed to help users to find out what is the
  2.    escape sequences sent by a particular key.  Users can use this to set up
  3.    their configuration files.  */
  4.  
  5. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Written by Tudor Hulubei and Andrei Pitis.  */
  22.  
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27.  
  28. #include <stdio.h>
  29.  
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #else /* !HAVE_STDLIB_H */
  33. #include "ansi_stdlib.h"
  34. #endif /* !HAVE_STDLIB_H */
  35.  
  36. #include <sys/types.h>
  37. #include <sys/ioctl.h>
  38.  
  39.  
  40. #ifdef HAVE_POSIX_TTY
  41. #include <termios.h>
  42. #else
  43. #ifdef HAVE_SYSTEMV_TTY
  44. #include <termio.h>
  45. #else
  46. #include <sgtty.h>
  47. #endif
  48. #endif
  49.  
  50. #include <signal.h>
  51.  
  52. #ifdef HAVE_UNISTD_H
  53. #include <unistd.h>
  54. #endif /* HAVE_UNISTD_H */
  55.  
  56. #include "stdc.h"
  57. #include "xio.h"
  58.  
  59.  
  60. /* Stolen from GNU Emacs.  */
  61. #ifdef _POSIX_VDISABLE
  62. #define CDISABLE _POSIX_VDISABLE
  63. #else /* not _POSIX_VDISABLE */
  64. #ifdef CDEL
  65. #undef CDISABLE
  66. #define CDISABLE CDEL
  67. #else /* not CDEL */
  68. #define CDISABLE 255
  69. #endif /* not CDEL */
  70. #endif /* not _POSIX_VDISABLE */
  71.  
  72.  
  73. #ifdef HAVE_LINUX
  74. speed_t ospeed;
  75. #else   /* !HAVE_LINUX */
  76. short ospeed;
  77. #endif  /* !HAVE_LINUX */
  78.  
  79.  
  80. #define TTY_OUTPUT 1
  81.  
  82.  
  83. #ifdef HAVE_POSIX_TTY
  84. static struct termios old_term;
  85. static struct termios new_term;
  86. #else
  87. #ifdef HAVE_SYSTEMV_TTY
  88. static struct termio old_term;
  89. static struct termio new_term;
  90. #else
  91. static struct sgttyb  old_arg;
  92. static struct tchars  old_targ;
  93. static struct ltchars old_ltarg;
  94. static struct sgttyb  new_arg;
  95. static struct tchars  new_targ;
  96. static struct ltchars new_ltarg;
  97.  
  98. /* NextStep doesn't define TILDE.  */
  99. #ifndef TILDE
  100. #define TILDE 0
  101. #endif
  102.  
  103. #endif /* HAVE_SYSTEMV_TTY */
  104. #endif /* HAVE_POSIX_TTY */
  105.  
  106.  
  107. RETSIGTYPE do_exit PROTO ((int));
  108.  
  109.  
  110. void
  111. tty_init()
  112. {
  113.     /* This simply doesn't fit into the current scheme.  Maybe the tt
  114.        stuff should be moved into a separate library.  This is clearly
  115.        a self contained function.  */
  116.     {
  117. #ifdef HAVE_POSIX_TTY
  118.     tcgetattr(TTY_OUTPUT, &old_term);
  119.  
  120.     signal(SIGTERM, do_exit);
  121.     signal(SIGQUIT, do_exit);
  122.     signal(SIGINT,  do_exit);
  123.  
  124.     new_term = old_term;
  125.     new_term.c_iflag &= ~(IXON | ICRNL | IGNCR | INLCR | IGNBRK | BRKINT);
  126.     new_term.c_oflag &= ~OPOST;
  127.     new_term.c_lflag |= ISIG | NOFLSH;
  128.     new_term.c_lflag &= ~(ICANON | ECHO);
  129.  
  130.     /* I think we will always have these ones:  */
  131.  
  132.     new_term.c_cc[VINTR] = CDISABLE;
  133.     new_term.c_cc[VQUIT] = CDISABLE;
  134.  
  135. #ifdef VSTART
  136.     new_term.c_cc[VSTART] = CDISABLE;
  137. #endif
  138.  
  139. #ifdef VSTOP
  140.     new_term.c_cc[VSTOP]  = CDISABLE;
  141. #endif
  142.  
  143.     new_term.c_cc[VMIN]  = 1;
  144.     new_term.c_cc[VTIME] = 0;
  145.  
  146.     /* ... but not always these ones: (in fact I am not sure if I
  147.        really need to overwrite all these, but just in case... */
  148.  
  149. #ifdef VERASE
  150.     new_term.c_cc[VERASE] = CDISABLE;
  151. #endif
  152.  
  153. #ifdef VKILL
  154.     new_term.c_cc[VKILL] = CDISABLE;
  155. #endif
  156.  
  157. #ifdef VEOL
  158.     new_term.c_cc[VEOL] = CDISABLE;
  159. #endif
  160.  
  161. #ifdef VEOL2
  162.     new_term.c_cc[VEOL2] = CDISABLE;
  163. #endif
  164.  
  165. #ifdef VSWTCH
  166.     new_term.c_cc[VSWTCH] = CDISABLE;
  167. #endif
  168.  
  169. #ifdef VSUSP
  170.     new_term.c_cc[VSUSP] = CDISABLE;
  171. #endif
  172.  
  173. #ifdef VDSUSP
  174.     new_term.c_cc[VDSUSP] = CDISABLE;
  175. #endif
  176.  
  177. #ifdef VREPRINT
  178.     new_term.c_cc[VREPRINT] = CDISABLE;
  179. #endif
  180.  
  181. #ifdef VDISCARD
  182.     new_term.c_cc[VDISCARD] = CDISABLE;
  183. #endif
  184.  
  185. #ifdef VWERASE
  186.     new_term.c_cc[VWERASE] = CDISABLE;
  187. #endif
  188.  
  189. #ifdef VLNEXT
  190.     new_term.c_cc[VLNEXT] = CDISABLE;
  191. #endif
  192.  
  193.     tcsetattr(TTY_OUTPUT, TCSADRAIN, &new_term);
  194.     ospeed = cfgetospeed(&new_term);
  195. #else
  196. #ifdef HAVE_SYSTEMV_TTY
  197.     ioctl(TTY_OUTPUT, TCGETA, &old_term);
  198.  
  199.     signal(SIGTERM, do_exit);
  200.     signal(SIGQUIT, do_exit);
  201.     signal(SIGINT,  do_exit);
  202.  
  203.     new_term = old_term;
  204.     new_term.c_iflag &= ~(IXON | ICRNL | IGNCR | INLCR);
  205.     new_term.c_oflag = 0;
  206.     new_term.c_lflag = 0;
  207.  
  208.     /* I think we will always have these:  */
  209.  
  210.     new_term.c_cc[VINTR]  = CDISABLE;
  211.     new_term.c_cc[VQUIT]  = CDISABLE;
  212.  
  213. #ifdef VSTART
  214.     new_term.c_cc[VSTART] = CDISABLE;
  215. #endif
  216.  
  217. #ifdef VSTOP
  218.     new_term.c_cc[VSTOP]  = CDISABLE;
  219. #endif
  220.  
  221.     new_term.c_cc[VMIN]   = 1;
  222.     new_term.c_cc[VTIME]  = 0;
  223.  
  224.     /* ... but not always these:  (in fact I am not sure if I really
  225.        need to overwrite all these, but just in case... */
  226.  
  227. #ifdef VERASE
  228.     new_term.c_cc[VERASE] = CDISABLE;
  229. #endif
  230.  
  231. #ifdef VKILL
  232.     new_term.c_cc[VKILL] = CDISABLE;
  233. #endif
  234.  
  235. #ifdef VEOL
  236.     new_term.c_cc[VEOL] = CDISABLE;
  237. #endif
  238.  
  239. #ifdef VEOL2
  240.     new_term.c_cc[VEOL2] = CDISABLE;
  241. #endif
  242.  
  243. #ifdef VSWTCH
  244.     new_term.c_cc[VSWTCH] = CDISABLE;
  245. #endif
  246.  
  247. #ifdef VSUSP
  248.     new_term.c_cc[VSUSP] = key_SUSPEND;             /* Ctrl-Z */
  249. #endif
  250.  
  251. #ifdef VDSUSP
  252.     new_term.c_cc[VDSUSP] = CDISABLE;
  253. #endif
  254.  
  255. #ifdef VREPRINT
  256.     new_term.c_cc[VREPRINT] = CDISABLE;
  257. #endif
  258.  
  259. #ifdef VDISCARD
  260.     new_term.c_cc[VDISCARD] = CDISABLE;
  261. #endif
  262.  
  263. #ifdef VWERASE
  264.     new_term.c_cc[VWERASE] = CDISABLE;
  265. #endif
  266.  
  267. #ifdef VLNEXT
  268.     new_term.c_cc[VLNEXT] = CDISABLE;
  269. #endif
  270.  
  271.     ioctl(TTY_OUTPUT, TCSETAW, &new_term);
  272.     ospeed = new_term.c_cflag & CBAUD;
  273. #else
  274.     ioctl(TTY_OUTPUT, TIOCGETP, &old_arg);
  275.     ioctl(TTY_OUTPUT, TIOCGETC, &old_targ);
  276.     ioctl(TTY_OUTPUT, TIOCGLTC, &old_ltarg);
  277.  
  278.     signal(SIGTERM, do_exit);
  279.     signal(SIGQUIT, do_exit);
  280.     signal(SIGINT,  do_exit);
  281.  
  282.     new_arg   = old_arg;
  283.     new_targ  = old_targ;
  284.     new_ltarg = old_ltarg;
  285.     new_arg.sg_flags = ((old_arg.sg_flags &
  286.              ~(ECHO | CRMOD | XTABS | ALLDELAY | TILDE)) | CBREAK);
  287.     new_targ.t_intrc   = CDISABLE;
  288.     new_targ.t_quitc   = CDISABLE;
  289.     new_targ.t_stopc   = CDISABLE;
  290.     new_targ.t_startc  = CDISABLE;
  291.     new_targ.t_eofc    = CDISABLE;
  292.     new_targ.t_brkc    = CDISABLE;
  293.     new_ltarg.t_lnextc = CDISABLE;
  294.     new_ltarg.t_flushc = CDISABLE;
  295.     new_ltarg.t_werasc = CDISABLE;
  296.     new_ltarg.t_rprntc = CDISABLE;
  297.     new_ltarg.t_dsuspc = CDISABLE;    /* DSUSPC (delayed SUSPC, ^Y).  */
  298.     new_ltarg.t_suspc  = CDISABLE;
  299.  
  300.     ioctl(TTY_OUTPUT, TIOCSETN, &new_arg);
  301.     ioctl(TTY_OUTPUT, TIOCSETC, &new_targ);
  302.     ioctl(TTY_OUTPUT, TIOCSLTC, &new_ltarg);
  303.     ospeed = new_arg.sg_ospeed;
  304. #endif /* HAVE_SYSTEMV_TTY */
  305. #endif /* HAVE_POSIX_TTY */
  306.  
  307. /* Try to make sure the terminal is not locked.  */
  308. #ifdef TCXONC
  309. #ifdef __QNX__
  310.     {
  311.         int value = 1;
  312.         ioctl (TTY_OUTPUT, TCXONC, &value);
  313.     }
  314. #else
  315.     ioctl(TTY_OUTPUT, TCXONC, 1);
  316. #endif
  317. #endif
  318.  
  319. #ifndef APOLLO
  320. #ifdef TIOCSTART
  321.     ioctl(TTY_OUTPUT, TIOCSTART, 0);
  322. #endif
  323. #endif
  324.  
  325. #ifdef HAVE_POSIX_TTY
  326. #ifdef TCOON
  327.     tcflow(TTY_OUTPUT, TCOON);
  328. #endif
  329. #endif
  330.     }
  331. }
  332.  
  333.  
  334. void
  335. tty_end()
  336. {
  337. #ifdef HAVE_POSIX_TTY
  338.     tcsetattr(TTY_OUTPUT, TCSADRAIN, &old_term);
  339. #else
  340. #ifdef HAVE_SYSTEMV_TTY
  341.     ioctl(TTY_OUTPUT, TCSETAW, &old_term);
  342. #else
  343.     ioctl(TTY_OUTPUT, TIOCSETN, &old_arg);
  344.     ioctl(TTY_OUTPUT, TIOCSETC, &old_targ);
  345.     ioctl(TTY_OUTPUT, TIOCSLTC, &old_ltarg);
  346. #endif /* HAVE_SYSTEMV_TTY */
  347. #endif /* HAVE_POSIX_TTY */
  348. }
  349.  
  350.  
  351. RETSIGTYPE
  352. do_exit(signum)
  353.     int signum;
  354. {
  355.     tty_end();
  356.     exit(1);
  357. }
  358.  
  359.  
  360. int
  361. main()
  362. {
  363.     char c;
  364.  
  365. #ifdef HAVE_GCC
  366.     printf(PRODUCT" "VERSION" - Display key sequence utility\n");
  367. #else
  368.     printf("GNU Interactive Tools 4.3.11 - Display key sequence utility\n");
  369. #endif /* !HAVE_GCC */
  370.     printf("GIT is free software; you can redistribute it and/or modify it under the\n");
  371.     printf("terms of the GNU General Public License as published by the Free Software\n");
  372.     printf("Foundation; either version 2, or (at your option) any later version.\n");
  373.     printf("Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.\n");
  374.     printf("Written by Tudor Hulubei and Andrei Pitis, students at PUB, Romania\n");
  375.  
  376.     printf("\nPress space when done.\n\n");
  377.  
  378.     tty_init();
  379.  
  380. #ifdef SIGTSTP
  381.     signal(SIGTSTP, SIG_IGN);
  382. #endif
  383.  
  384. #ifdef SIGCONT
  385.     signal(SIGCONT, SIG_IGN);
  386. #endif
  387.  
  388.     for (;;)
  389.     {
  390.     read(0, &c, 1);
  391.  
  392.     if (c == ' ')
  393.         break;
  394.  
  395.     printf("%x ", c);
  396.     fflush(stdout);
  397.     }
  398.  
  399.     tty_end();
  400.  
  401.     printf("\n");
  402.  
  403.     return 0;
  404. }
  405.